<?php

namespace app\api\controller\course;

use app\api\controller\Controller;
use app\common\model\CourseCard;
use app\common\model\video\Course;
use think\Cache;
use think\Db;
use app\common\model\Banner;
use app\api\model\store\Shop as ShopModel;

/**
 * 拳击首页数据展示
 * Class Order
 * @package app\api\controller\shop
 */
class Index extends Controller
{
   

    /**
     * 首页数据
     */
    public function index()
    {
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');

        // banner图
        $BannerModel = new Banner();
        $BannerList = $BannerModel->getList();

        //门店数据
        $ShopModel = new ShopModel;
        $ShopList = $ShopModel->getList();
        foreach ($ShopList as $k=>$v){
            //获取店家logo
            $shop_logo_info = Db::name('upload_file')->where(['file_id'=>$v['logo_image_id']])->find();
            if($shop_logo_info){
                //返回上传图片路径
                $service = $_SERVER['SERVER_NAME'];//域名
                $ShopList[$k]['shop_logo'] = base_url()."/uploads/".$shop_logo_info['file_name'];
            }else{
                $ShopList[$k]['shop_logo'] = '';
            }
        }

        // 会员卡列表
        $CardList = $this->Card([],1);

        return $this->renderSuccess([
            'BannerList' => $BannerList,  // 轮播图
            'ShopList' => $ShopList,  // 门店
            'CardList' => $CardList,  // 会员卡列表
        ]);
    }

    //门店// 会员卡列表
    public function shopcard(){
        $param = request()->param();

        $CardList = $this->Card($param);
        return $this->renderSuccess([
            'CardList' => $CardList,  // 会员卡列表
        ]);
    }


    // 会员卡公用方法
    public function Card($Where = [],$is_recommend=''){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');

        // 商品列表获取条件
        $params = array_merge([
            'type' => 0,         // 卡分类
            'shop_id' => 0,         // 店家
        ], $Where);

        $filter = [];
        $params['type'] > 0 && $filter['type'] = $params['type'];
        $params['shop_id'] > 0 && $filter['shop_id'] = $params['shop_id'];

        $where = [];
        $is_recommend != '' && $where['c.is_recommend'] = 1;
        //会员卡列表
        $CardList = Db::name('course_card')
            ->alias('c')
            ->join('course_card_image i' ,'c.id = i.course_card_id','left')
            ->join('upload_file u' ,'i.image_id = u.file_id','left')
            ->field('c.*,u.file_name')
            ->group('c.id')
            ->where(['c.wxapp_id'=>$wxapp_id,'c.is_delete'=>0,'c.status'=>10])
            ->where($where)
            ->where($filter)
            ->select()
            ->toarray();

        foreach ($CardList as $key=>$val){
            if($val['file_name'] && $val['file_name'] != ''){
                //返回上传图片路径
                $service = $_SERVER['SERVER_NAME'];//域名
                $CardList[$key]['file_name'] = base_url()."/uploads/".$val['file_name'];
            }else{
                $CardList[$key]['file_name'] = '';
            }
        }
        return $CardList;
    }


    //会员卡详情
    public function CardDetail(){
        $param = request()->param('card_id');
        //会员卡列表
        $CardInfo = CourseCard::detail($param);
        $CardInfo['file_name'] = $CardInfo['image'][0]['file_path'];
//        $CardInfo = Db::name('course_card')
//            ->alias('c')
//            ->join('course_card_image i' ,'c.id = i.course_card_id','left')
//            ->join('upload_file u' ,'i.image_id = u.file_id','left')
//            ->field('c.*,u.file_name')
//            ->where(['c.id'=>$param])
//            ->find();
//        $CardInfo['file_name'] = "http://".$_SERVER['HTTP_HOST']."/uploads/".$CardInfo['file_name'];
        return $this->renderSuccess([
            'CardInfo' => $CardInfo,  // 会员卡详情
        ]);
    }


    //门店详情
    public function shopDetail(){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');
        //门店id
        $shop_id = $request->param('shop_id');
        //获取门店信息
        //门店数据
//        $ShopInfo = Db::name('store_shop')
//            ->alias('s')
//            ->join('upload_file u' ,'s.logo_image_id = u.file_id','left')
//            ->where(['s.shop_id'=>$shop_id])
//            ->field('s.*,u.file_name')
//            ->find();
//        //返回上传图片路径
//        $service = $_SERVER['SERVER_NAME'];//域名
//        $ShopInfo['file_name'] = 'https://'.$service."/uploads/".$ShopInfo['file_name'];
        $ShopInfo = ShopModel::detail($shop_id);

        //门店教练
        $Coach = Db::name('coach')
            ->alias('c')
            ->join('user r' ,'c.user_id = r.user_id','left')
            ->join('upload_file u' ,'c.image_id = u.file_id')
            ->where(['c.shop_id'=>$ShopInfo['shop_id']])
            ->where(['c.is_delete'=>0])
            ->field('c.*,r.avatarUrl,u.file_name')
            ->select()->toArray();
        foreach ($Coach as $k=>$v){
            $Coach[$k]['avatarUrl'] = base_url().'/uploads/'.$v['file_name'];
//            if (preg_match('/(uploads)/',$v['avatarUrl'])){
//                $Coach[$k]['avatarUrl'] = base_url().$v['avatarUrl'];
//            }
        }
        $ShopInfo['coach'] = $Coach;
        $videoModel = new Course();
        $video = $videoModel->getList(['status'=>1],['image'],3);


        return $this->renderSuccess([
            'ShopInfo' => $ShopInfo,  // 店铺详情
            'video'=>$video
        ]);
    }

    //教练详情
    public function coachDetail(){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');
        //教练id
        $coach_id = $request->param('coach_id');
        //获取教练信息
        //门店教练
        $Coach = Db::name('coach')
            ->alias('c')
            ->join('user r' ,'c.user_id = r.user_id','left')
            ->join('upload_file u' ,'c.image_id = u.file_id')
            ->where(['c.coach_id'=>$coach_id])
            ->field('c.*,r.avatarUrl,u.file_name')
            ->find();
        $Coach['avatarUrl'] = base_url()."/uploads/".$Coach['file_name'];
//        if (preg_match('/(uploads)/',$Coach['avatarUrl'])){
//            $Coach['avatarUrl'] = base_url().$Coach['avatarUrl'];
//        }

        return $this->renderSuccess([
            'Coach' => $Coach,  // 教练详情
        ]);
    }

    //课程列表
    public function CourseList(){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');
        //课程类型id
        $type = $request->param('type');

        //门店id
        $shop_id = $request->param('shop_id');

        $where = [];
        if($type){
            $where['type'] = $type;
        }
        if($shop_id){
            $where['shop_id'] = $shop_id;
        }

        //获取课程信息
        $Course = Db::name('course')
            ->alias('c')
            ->join('course_image i' ,'c.id = i.course_id','left')
            ->join('upload_file u' ,'i.image_id = u.file_id')
            ->group('c.id')
            ->where(['c.wxapp_id'=>$wxapp_id,'c.is_delete'=>0,'c.status'=>10])
            ->where($where)
            ->field('c.*,u.file_name')
            ->select()
            ->toArray();
        foreach ($Course as $key=>$val){
            if($val['file_name'] && $val['file_name'] != ''){
                //返回上传图片路径
                $service = $_SERVER['SERVER_NAME'];//域名
                $Course[$key]['file_name'] = base_url()."/uploads/".$val['file_name'];
            }else{
                $Course[$key]['file_name'] = '';
            }
        }

        return $this->renderSuccess([
            'Course' => $Course,  // 课程列表
        ]);
    }

    //课程详情
    public function coursedetail(){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');
        //课程id
        $id = $request->param('id');

        //获取课程信息
        $CourseInfo = Db::name('course')
            ->alias('c')
            ->join('course_image i' ,'c.id = i.course_id','left')
            ->join('upload_file u' ,'i.image_id = u.file_id')
            ->where(['c.id'=>$id])
            ->field('c.*,u.file_name')
            ->find();

        //type   1次卡、2团课   1次卡拉出所有教练
        if($CourseInfo['type'] == 1){
            //门店教练全部
            $Coach = Db::name('coach')
                ->alias('c')
                ->join('user r' ,'c.user_id = r.user_id','left')
                ->join('upload_file u' ,'c.image_id = u.file_id')
                ->where(['c.shop_id'=>$CourseInfo['shop_id'],'c.is_delete'=>0])
                ->field('c.*,r.avatarUrl,u.file_name')
                ->select()->toArray();
        }else{
            //门店教练个人
            $Coach = Db::name('coach')
                ->alias('c')
                ->join('user r' ,'c.user_id = r.user_id','left')
                ->join('upload_file u' ,'c.image_id = u.file_id')
                ->where(['c.coach_id'=>$CourseInfo['coach_id'],'c.is_delete'=>0])
                ->field('c.*,r.avatarUrl,u.file_name')
                ->select()->toArray();

        }
        foreach ($Coach as $k=>$v){
            $Coach[$k]['avatarUrl'] = base_url().'/uploads/'.$v['file_name'];
        }
        $CourseInfo['coach'] = $Coach;

        if($CourseInfo['file_name'] && $CourseInfo['file_name'] != ''){
            //返回上传图片路径
            $service = $_SERVER['SERVER_NAME'];//域名
            $CourseInfo['file_name'] = base_url()."/uploads/".$CourseInfo['file_name'];
        }else{
            $CourseInfo['file_name'] = '';
        }

        return $this->renderSuccess([
            'CourseInfo' => $CourseInfo,  // 课程详情
        ]);
    }
    
        // 会员卡公用方法
    public function Card1($Where = [],$is_recommend=''){
        //必须请求的参数
        $request = Request();
        $wxapp_id = $request->param('wxapp_id');

        // 商品列表获取条件
        $params = array_merge([
            'type' => 0,         // 卡分类
            'shop_id' => 0,         // 店家
        ], $Where);

        $filter = [];
        $params['type'] > 0 && $filter['type'] = $params['type'];
        $params['shop_id'] > 0 && $filter['shop_id'] = $params['shop_id'];

        $where = [];
        $is_recommend != '' && $where['c.is_recommend'] = 1;
        //会员卡列表
        $CardList = Db::name('course_card')
            ->alias('c')
            ->join('course_card_image i' ,'c.id = i.course_card_id')
            ->join('upload_file u' ,'i.image_id = u.file_id')
            ->field('c.id,u.file_name')
            //->group('c.id')
            ->where(['c.wxapp_id'=>$wxapp_id,'c.is_delete'=>0])
            ->where($where)
            ->where($filter)
            ->select()
            ->toarray();
        return $this->renderSuccess([
            'CourseInfo' => $CardList,  // 课程详情
        ]);
    }

    /**
     * @title 视频课程列表
     * @description
     * @url
     * @method
     * @param name: type: require:1 default: desc:
     * @return
     * @author Qian
     * Time: 2023/4/7  13:48
     */
    public function VideoList(){
        $videoModel = new Course();
        $video = $videoModel->getList(['status'=>1],['image'],10);
        return $this->renderSuccess(compact('video'));
    }

    /**
     * @title 视频课程详情
     * @param $id
     * @author Qian
     * Time: 2023/4/7  13:46
     */
    public function VideoDetail($id){
        $detail = Course::detail($id);
        return $this->renderSuccess(compact('detail'));
    }

}